<?php 
    //fetches all of the docent from the table.
    function fetch_docent(){
        $result = mysql_query("SELECT `docent_id` AS `id`,`docent_username` AS `username` FROM `docent_profiel`");
        
        $docent = array(); // empty array

        // mysql_fetch_assoc fetch a result row as an associative array
        while (($row = mysql_fetch_assoc($result)) !== false){
            $docent[] = $row; // add this to the end of the array
        }
        
        return $docent; // return the a array
    }

 //fetches profile information for the given docent from the database.
    function fetch_docent_info($uid){
           $uid = (int)$uid; // url bar in the browser need to be an number

        $sql = "SELECT 
                    `docent_id`         AS `id`,
                    `docent_username`   AS `username`,
                    `docent_firstname`  AS `firstname`,
                    `docent_lastname`   AS `lastname`,
                    `docent_email`      AS `email`,
                    `docent_about`      AS `about`,
                    `docent_location`   AS `location`,
                    `docent_gender`     AS `gender`
                    FROM `docent_profiel`
            WHERE `docent_id` = {$uid}";
            
        // Send a mysql query
        $result = mysql_query($sql);
        //create a associative array
        $info = mysql_fetch_assoc($result);
        
        $info['afbeelding'] = (file_exists("{$GLOBALS['path']}/docent_afbeelding/{$info['id']}.jpg")) ? "core/docent_afbeelding/{$info['id']}.jpg" : "core/docent_afbeelding/default.jpg";
        
        return $info;
    }
    
    // Werkt de informatie van de docent_profiel bij 1
    function set_profile_info($email, $about, $location, $afbeelding){
        $email    =  mysql_real_escape_string(htmlentities($email));
        $about    =  mysql_real_escape_string(nl2br(htmlentities($about)));
        $location =  mysql_real_escape_string(htmlentities($location));
        //$phone    =  mysql_real_escape_string(htmlentities($phone));

        // Geuploade foto wordt gecontrolleerd op basis van voorwaarden
        if(file_exists($afbeelding))
        {
            // grootte van de afbeelding wordt hier opgevraagd en opgeslagen in $src_size om de grootte te bepalen
            $src_size = getimagesize($afbeelding);
            
            /**
            * The MIME type ........ is used to denote the presence of images compressed and stored in the....format
            * imagecreatefromjpeg ? Create a new image from file or URL
            */
            
            if($src_size['mime'] === 'image/jpeg'){
                $src_img = imagecreatefromjpeg($afbeelding);
                
            } else if ($src_img['mime'] === 'image/png') {
                $src_img = imagecreatefrompng($afbeelding);
                
            } else if ($src_img['mime'] === 'image/gif'){
                $src_img = imagecreatefromgif($afbeelding);
            } else {
                // als de bovenstaande formaten niet zijn opgegeven is dat false
                $src_img = false;
            }

                
            if ($src_img !== false){
                $thumb_width = 100;
                // 0 width of the image in pixel
                if($src_size[0] <= $thumb_width){
                    $thumb = $src_img;
                } else{
                    // als geuploade foto de gegeven groote heeft, 
                    $new_size[0] = $thumb_width;
                    $new_size[1] = ($src_size[1] /*hooge*/ / $src_size[0] /*breedte*/) * $thumb_width;

                   // imagecreatetruecolor ? Create a new true color image
                    $thumb = imagecreatetruecolor($new_size[0], $new_size[1]);
                    //imagecopyresampled ? Copy and resize part of an image with resampling
                    imagecopyresampled($thumb, $src_img, 0, 0, 0, 0, $new_size[0], $new_size[1], $src_size[0], $src_size[1]);

                }
                
                imagejpeg($thumb, "{$GLOBALS['path']}/docent_afbeelding/{$_SESSION['uid']}.jpg");
            }
        }
        
        // Gegevens dcent profiel wordt hiermee bijgewerkt 2
        $sql = "UPDATE `docent_profiel` SET
            `docent_email` = '$email', 
            `docent_about` = '$about',
            `docent_location` = '$location'
            WHERE `docent_id` = {$_SESSION['uid']}";

        mysql_query($sql);
                

    }
    
?>